﻿using System;
using System.Linq;

using Carbon.Web.Forms;
using Carbon.Web.Forms.jQueryForms;

namespace Demo.Demos
{
    [Serializable]
    public class ViewModelEntityFramework : IDisposable
    {
        #region Fields (5)
        private DropDownListItemCollection<Company, long?> m_CompanyHeWorkedForDropDown = default(DropDownListItemCollection<Company, long?>);

        [NonSerialized]
        private DemoDao m_Dao = default(DemoDao);

        private bool m_CompanyHeWorkedForDropDown_calculated = false;
        [NonSerialized]
        private bool m_Dao_calculated = false;
        private bool m_Diposed = false;
        #endregion

        #region Properties (8)
        public FileReference Avatar { get; set; }
        public Company Company { get; set; }
        public bool CompanyChosen
        {
            get
            {
                return Company != null;
            }
        }
        public DropDownListItemCollection<Company, long?> CompanyHeWorkedForDropDown
        {
            get
            {
                if (!m_CompanyHeWorkedForDropDown_calculated)
                {
                    m_CompanyHeWorkedForDropDown = new DropDownListItemCollection<Company, long?>(
                        Dao.Database.Companies.ToArray(),
                        i => i.Id,
                        i => i.Name,
                        Person.CompanyHeWorkedFor,
                        true);
                    m_CompanyHeWorkedForDropDown_calculated = true;
                }

                return m_CompanyHeWorkedForDropDown;
            }
            set
            {
                if (m_CompanyHeWorkedForDropDown.SelectedKey != value.SelectedKey)
                {
                    if (value.SelectedKey != null)
                    {
                        Company = Dao.Database.Companies.Where(i => i.Id == value.SelectedKey).Single();
                    }
                    else
                    {
                        Company = null;
                    }
                }

                m_CompanyHeWorkedForDropDown = value;
                m_CompanyHeWorkedForDropDown_calculated = true;
            }
        }
        public DemoDao Dao
        {
            get
            {
                if (!m_Dao_calculated)
                {
                    m_Dao = new DemoDao();
                    m_Dao_calculated = true;
                }

                return m_Dao;
            }
        }
        public FileReferenceCollection Files { get; set; }
        public bool HasAvatar
        {
            get
            {
                return Avatar != null;
            }
        }
        public EfPerson Person { get; set; }
        #endregion

        #region Constructors (2)
        public ViewModelEntityFramework()
        {
            Person = new EfPerson();
        }

        public ViewModelEntityFramework(DemoDao dao, EfPerson person)
        {
            Person = person;
            if (person == null) throw new NullReferenceException("person");

            dao.Database.DeleteObject(person);
        }
        #endregion

        #region Methods (2)

        public void Dispose()
        {
            if (m_Diposed) throw new NotSupportedException("Already Disposed");

            if (m_Dao_calculated)
                m_Dao.Dispose();

            m_Diposed = true;
        }

        public void SaveToDatabase()
        {
            Person.CompanyHeWorkedFor = CompanyHeWorkedForDropDown.SelectedItem.Id;
            if (Person.EntityState == System.Data.EntityState.Added)
            {
                this.Dao.Database.Persons.AddObject(Person);
                this.Dao.Database.SaveChanges();
                this.Dao.Database.Detach(Person);
            }
            else
            {
                this.Dao.Database.Persons.Attach(Person);
                this.Dao.Database.SaveChanges();
                this.Dao.Database.Detach(Person);
            }
        }

        #endregion
    }
}